var myFunc = {};

this.km = context.getSystemService(context.KEYGUARD_SERVICE);

myFunc.isLock = function () {
    let lock = km.inKeyguardRestrictedInputMode();
    log("isLock: ", lock)
    return lock;
}

myFunc.clickCenter = function (comBox, isRandom, addX, addY) {

    let x = comBox.bounds().centerX();
    let y = comBox.bounds().centerY();
    if (!isRandom) {
        x = x + random(-10, 10);
        y = y + random(-10, 10);
    }
    if (addX) {
        x = x + addX;
    }
    if (addY) {
        y = y + addY;
    }
    log("clickCenter", x, y)
    click(x, y);
}

myFunc.clickR = function (x, y) {
    x = x + random(-10, 10);
    y = y + random(-10, 10);
    click(x, y);
}

importClass(android.net.ConnectivityManager);
importClass(android.content.Context);
function checkNetwork() {
    var cm = context.getSystemService(Context.CONNECTIVITY_SERVICE);
    var networkInfo = cm.getActiveNetworkInfo();
    
    return networkInfo != null && networkInfo.isConnected();
}
function isPingable() {
    try {
        var process = java.lang.Runtime.getRuntime().exec("ping -c 1 www.baidu.com");
        var status = process.waitFor();
        return status == 0;
    } catch (e) {
        return false;
    }
}
myFunc.isPingSuccess = function () {
    // 首选系统服务方式
    var hasNetwork = checkNetwork();
    
    // 如果系统服务检测失败,使用 HTTP 请求作为备选
    if (!hasNetwork) {
        hasNetwork = isNetworkAvailable(); 
    }
    
    return hasNetwork;
}

myFunc.tuichuAll = function () {
    home();
    sleep(500);
    recents();
    sleep(500);
    // className("android.widget.ImageView").desc("关闭所有最近打开的应用").findOne(5000).click();
    this.clickByText('清除');
    log("关闭应用");
}

myFunc.tuichu = function (appName) {
    var name = getPackageName(appName)
    var cName = currentPackage();
    log("退出:" + appName, name, "当前", cName);
    if (cName != name) {
        if (appName == "微信") {
            home();
            app.launchApp(appName);
            sleep(1000);
            var a = textContains("微信").find()[0];
            this.clickCenter(a);
            sleep(1000);
        } else {
            app.launchApp(appName);
            sleep(1000);
        }
    }
    while (true) {
        back();
        sleep(400);
        if (currentPackage() !== name) {
            log("退出:" + appName);
            break;
        }
    }
}

myFunc.openWifiOrData = function (useWiFi) {
    dealWifiOrData(useWiFi, true);
}

function dealWifiOrData(useWiFi, doOpen) {
    if (myFunc.findOne('移动数据') == null) {
        notifications();
        sleep(1000);
    }
    var isOpen = false;
    var btn = null;
    var word = '';
    if (useWiFi) {
        word = "WLAN,";
    } else {
        word = "移动数据,";
    }
    //报错就退出
    btn = descContains(word).findOne(1000);
    if (btn.text() == "开启") {
        isOpen = true;
    }

    if ((doOpen && !isOpen) || (!doOpen && isOpen)) {
        myFunc.clickCenter(btn)
    }

}

function oldDealWifiOrData(useWiFi, doOpen) {
    if (useWiFi) {
        app.startActivity({
            action: "android.net.wifi.PICK_WIFI_NETWORK",

            flag: 'Intent.FLAG_ACTIVITY_NEW_TASK'
        });
    } else {
        // var intent = new Intent("android.intent.action.MAIN");
        // intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        // intent.setClassName("com.android.phone", "com.android.phone.MobileNetworkSettings");
        // intent.setClassName("com.android.phone", "com.android.simsettings.activity.OplusSimSettingsActivity");
        // app.startActivity(intent)
        //拉出通知栏
        notifications();
        sleep(1500);
        // press(294, 560, 1000);
        myFunc.clickByTextC("移动")
        return;
    }
    var openBut = idContains('switch_widget').findOne(5000);
    var open = openBut.checked();
    //开网 || 关网
    if ((doOpen && !open) || (!doOpen && open)) {
        if (openBut != null) {
            if (!openBut.click()) {
                sleep(800);
                myFunc.clickCenter(openBut);
            }
        } else if (useWiFi) {
            app.sendBroadcast(
                {
                    action: 'zsl.open.wifi'
                }
            );
            var ack = text('允许').findOne(5000);
            if (ack != null) {
                ack.click();
            }
        } else {
            xialaWifi();
        }
    }
    if (doOpen) {
        sleep(5000);
    }
    // home();
}

function xialaWifi() {
    //拉出通知栏
    notifications();
    sleep(500);
    if (useWiFi) {
        //开WiFi
        click(135, 560);
        log("打开WiFi");
    } else {
        //开流量
        click(294, 560);
        log("打开流量");
    }
}

myFunc.closeWifiOrData = function (useWiFi) {
    dealWifiOrData(useWiFi, false);
}

myFunc.inputPassWord = function (pass) {
    log("开始唤醒屏幕");
    device.wakeUp();
    log("唤醒屏幕");
    if (myFunc.isLock()) {
        sleep(1500);
        swipe(600, 2100, 600, 300, 500);
        swipe(600, 2100, 600, 300, 500);
        swipe(600, 2100, 600, 300, 500);
        //1坐标 210,1390 +330,240
        if (pass == null) {
            pass = '159987';
        }
        if (desc(pass.substring(0, 1)).findOnce() == null) {
            click(114, 1778);
        }
        for (a of pass) {
            myFunc.clickCenter(desc(a).findOnce());
            sleep(400);
        }
        log("密码输入完成");
    } else {
        log("屏幕亮着");
    }
}


myFunc.closeAppEx = function (packageName) {
    var name = getPackageName(packageName);
    if (!name) {
        if (getAppName(packageName)) {
            name = packageName;
        } else {
            return false;
        }
    }
    app.openAppSetting(name);
    text(packageName).findOne(3000)
    sleep(1500);
    let is_sure = this.findOnceTextDesc("强行停止", true);
    // let is_sure = textMatches(/(.*强.*|.*停.*|.*结.*|.*行.*)/).findOne(3000);
    if (is_sure.enabled()) {
        // textMatches(/(.*强.*|.*停.*|.*结.*|.*行.*)/).findOne(3000).click();
        is_sure.click();
        sleep(800);
        // textMatches(/(.*确.*|.*定.*)/).findOne().click();
        this.clickTextByBtn("强行停止", 1000);
        log(app.getAppName(name) + "应用已被关闭");
        sleep(500);
        back();
    } else {
        log(app.getAppName(name) + "应用不能被正常关闭或不在后台运行");
        back();
    }
}

myFunc.openMyApp = function (appName) {
    var appPackName = getPackageName(appName);
    if (appPackName == null) {
        toast(appName + "不存在");
    } else if (appPackName != currentPackage()) {
        home();
        sleep(500);

        // launch(appPackName)
        log("打开" + appName, launchApp(appName));
        // while(appPackName != currentPackage()) {
        //     sleep(500);
        // }
        sleep(2500);
        // waitForPackage(appPackName);
    } else if (textEndsWith("首页").exists()) {
        myFunc.textpClick("首页");
    }
    myFunc.clickByText('取消', 500)
    // var cancle = text("取消").findOnce();
    // if(cancle != null) {
    //     this.clickCenter(cancle);
    // }
    // for (let index = 0; index < 4; index++) {
    //     if (!scrollUp()) {
    //         break;
    //     }
    //     sleep(300)
    // }
}

myFunc.initNetWork = function (pass, useWiFi) {

    engines.all().map((ScriptEngine) => {
        var a = engines.myEngine().toString();
        var b = ScriptEngine.toString();
        log("运行当前脚本", a == b);
        if (a !== b) {
            ScriptEngine.forceStop();
        }
    });

    //判断屏幕是否打开
    myFunc.inputPassWord(pass);
    myFunc.memory();
    //判断网络，打开流量、WiFi
    for (let index = 0; index < 4; index++) {
        if (myFunc.isPingSuccess()) {
            return true;
        } else {
            log(useWiFi, "没网，等15S");
            myFunc.openWifiOrData(useWiFi);
            useWiFi = !useWiFi;
            sleep(15000)
        }
    }
    return false;
}

myFunc.clickText = function (textWord, time, fuzz, shoudian, error, roudom) {
    if (time == undefined) {
        time = 3000;
    }
    var a = null;
    if (fuzz != undefined || fuzz == 1 || fuzz == true) {
        a = textContains(textWord).findOne(time);
    } else {
        a = text(textWord).findOne(time);
    }
    if (a != null) {
        sleep(500);
        if (shoudian == false) {
            return a.click();
        } else {
            this.clickCenter(a, roudom);
            return true;
        }
    } else {
        if (error == undefined) {
            log('找不到', textWord);
        } else {
            throw '找不到' + textWord;
        }
    }
    return false;
}

myFunc.clickTextByBtn = function (textWord, time, fuzz) {
    return this.clickText(textWord, time, fuzz, false);
}

myFunc.clickDesc = function (textWord, time, fuzz, shoudian, error) {
    if (fuzz == undefined) {
        fuzz = false;
    }
    if (time == undefined) {
        time = 3000;
    }
    var a = null;
    if (fuzz) {
        a = descContains(textWord).findOne(time);
    } else {
        a = desc(textWord).findOne(time);
    }
    if (a != null) {
        if (shoudian == undefined) {
            this.clickCenter(a);
            return true;
        } else {
            return a.click();
        }
    } else {
        if (error == undefined) {
            log('找不到', textWord);
        } else {
            throw '找不到' + textWord;
        }
    }
    return false;
}



myFunc.clickByTextDescC = function (str) {
    var ret = this.clickText(str, 2000, true);
    if (!ret) {
        ret = this.clickDesc(str, 2000, true);
    }
    return ret;
}


myFunc.clickByTextDesc = function (str) {
    var ret = this.clickText(str, 2000);
    if (!ret) {
        ret = this.clickDesc(str, 2000);
    }
    return ret;
}


myFunc.clickByTextC = function (str, time, roudom) {
    return this.clickText(str, time, true, undefined, undefined, roudom);
}

myFunc.clickByText = function (str, time, roudom) {
    return this.clickText(str, time, undefined, undefined, undefined, roudom);
}


myFunc.bianli = function (a, ifLog, list, lv) {
    if(lv == undefined) {
        lv = 1
    }
    if (Array.isArray(a)) {
        for (var b of a) {
            if (ifLog != false) {
                log('listRoot:' + lv, b.id(), b.className(), "text:" + b.text(), "desc:" + b.desc(), "idx:" + b.indexInParent(),
                    "depth:" + b.depth(), "top:" + b.bounds().top, "left:" + b.bounds().left, "click:" + b.clickable(), "right:" + b.bounds().right, "bottom:" + b.bounds().bottom);
            }
            if (b.childCount() > 0) {
                var child = b.children();
                for (var element of child) {
                    if (element != null) {
                        if (ifLog != false) {
                            log('listItem:' + lv, element.id(), element.className(), "text:" + element.text(), "desc:" + element.desc(), "idx:" + element.indexInParent(),
                                "depth:" + element.depth(), "top:" + element.bounds().top, "left:" + element.bounds().left, "click:" + element.clickable(), "right:" + element.bounds().right, "bottom:" + element.bounds().bottom);
                        }
                        if (list != undefined) {
                            list.push(element);
                        }
                        this.bianli(element, ifLog, list, ++lv);
                    }
                };
            }
        }
    } else {
        if (ifLog != false) {
            log('root:' + lv, a.id(), a.className(), "text:" + a.text(), "desc:" + a.desc(), "idx:" + a.indexInParent(),
                "depth:" + a.depth(), "top:" + a.bounds().top, "left:" + a.bounds().left, "click:" + a.clickable(), "right:" + a.bounds().right, "bottom:" + a.bounds().bottom);
        }
        if (a.childCount() > 0) {
            var child = a.children();
            for (var element of child) {
                if (element != null) {
                    if (ifLog != false) {
                        log('Item:' + lv, element.id(), element.className(), "text:" + element.text(), "desc:" + element.desc(), "idx:" + element.indexInParent(),
                            "depth:" + element.depth(), "top:" + element.bounds().top, "left:" + element.bounds().left, "click:" + element.clickable(), "right:" + element.bounds().right, "bottom:" + element.bounds().bottom);
                    }
                    if (list != undefined) {
                        list.push(element);
                    }
                    this.bianli(element, ifLog, list, ++lv);
                }
            };
        }
    }
}

myFunc.getNumLimit = function (title) {
    var numIdx = title.substring(title.indexOf("(") + 1, title.indexOf(")")).split("/");
    var ret = [parseInt(numIdx[0]), parseInt(numIdx[1])];
    log(ret);
    return ret;
}


myFunc.log = function () {
    var len = arguments.length;
    var msg = "";
    for (var i = 0; i < len; i++) {
        msg = msg + " " + (arguments[i]);
    }
    toastLog(msg)
}

myFunc.logList = function (a) {
    a.forEach(element => {
        // log("----", element);
    
        log(element.id(), element.className(), "text:" + element.text(), "desc:" + element.desc(), "click:" + element.clickable(), 
        "idx:" + element.indexInParent(), "depth:" + element.depth(), "top:" + element.bounds().top, "left:" + element.bounds().left
        , "bounds:" + element.bounds() , "centerX:" + element.bounds().centerX() , "centerY:" + element.bounds().centerY() 
        )
    });
}



myFunc.textpClick = function (word, time, fuzzy) {
    if (time == undefined) {
        time = 3000;
    }
    var w = null;
    if (fuzzy == undefined) {
        w = text(word).findOne(time);
    } else {
        w = textContains(word).findOne(time);
    }
    if (w != null) {
        for (let index = 0; index < 3; index++) {
            w = w.parent();
            if (w.click()) {
                return true;
            }
        }
    }
    return false;

}

myFunc.textCpClick = function (word, time) {
    return this.textpClick(word, time, true);
}

myFunc.pClick = function (w) {
    if (w != null) {
        for (let index = 0; index < 3; index++) {
            w = w.parent();
            if (w.click()) {
                log("父节点点击", w.text());
                return true;
            }
        }
    }
    return false;
}


myFunc.reqScreen = function () {
    threads.start(function () {
        text("立即开始").findOne(3000).click();
    });
    if (!requestScreenCapture()) {
        toast("请求截图失败");
        exit();
    }
}


myFunc.tuichuToStr = function (str, time, fuzzy, throwError) {

    if (time == undefined) {
        time = 1000;
    }
    var home = 'com.huawei.android.launcher.unihome.UniHomeLauncher';
    var homePage = 'com.huawei.android.launcher';
    if (!Array.isArray(str)) {
        str = [str];
    }
    var flag = true;
    var idx = 0;
    while (flag) {
        var exists = true;
        for (let index = 0; index < str.length; index++) {
            var element = str[index];
            var ret = false;
            var theWord = text(element).findOne(800);
            if (fuzzy != undefined) {
                theWord = textContains(element).findOne(800);
            }
            if (theWord != null) {
                ret = true;
            }
            exists = exists & ret;
            log('查', element, exists);
            if (!exists) {
                break;
            }
        }
        if (exists) {
            break;
        } else if (idx > 10) {
            log('idx:' + idx);
            if (throwError) {
                throw "未找到:" + str;
            }
            break;
        } else {
            idx = idx + 1;
            back();
            sleep(time);
        }
    }
}


myFunc.tuichuToStrByOr = function (str, time, fuzzy) {
    if (time == undefined) {
        time = 1000;
    }
    var home = 'com.huawei.android.launcher.unihome.UniHomeLauncher';
    var homePage = 'com.huawei.android.launcher';
    if (!Array.isArray(str)) {
        str = [str];
    }
    var flag = true;
    var idx = 0;
    var exists = false;
    while (flag) {
        exists = false;

        for (let index = 0; index < str.length; index++) {
            var element = str[index];
            var ret = false;
            var theWord = text(element).findOne(800);
            if (fuzzy != undefined) {
                theWord = textContains(element).findOne(800);
            }
            if (theWord != null) {
                ret = true;
            }
            exists = exists | ret;
            log('查', element, exists);
            if (exists) {
                break;
            }
        }
        if (exists) {
            break;
        } else if (idx > 10) {
            log('idx:' + idx);
            break;
        } else {
            idx = idx + 1;
            back();
            sleep(time);
        }
    }
    return exists;
}

myFunc.memory = function () {
    var pid = android.os.Process.myPid();
    var a = [pid];
    var activityManager = context.getSystemService(context.ACTIVITY_SERVICE);
    var memoryInfoArray = activityManager.getProcessMemoryInfo(a);
    log("本应用当前使用了独占内存" + memoryInfoArray[0].getTotalPrivateDirty() / 1024 + "mb的内存");
    log("本应用当前使用了总内存" + memoryInfoArray[0].getTotalPss() / 1024 + "mb的内存");
    var runningAppProcessesList = activityManager
        .getRunningAppProcesses();

    runningAppProcessesList.forEach(runningAppProcessInfo => {
        // 进程ID号
        var pid = runningAppProcessInfo.pid;
        // 用户ID
        var uid = runningAppProcessInfo.uid;
        // 进程名
        var processName = runningAppProcessInfo.processName;
        // 占用的内存
        var pids = [pid];
        var memoryInfo = activityManager
            .getProcessMemoryInfo(pids);
        var memorySize = memoryInfo[0].dalvikPrivateDirty;

        log("processName=" + processName + ",pid=" + pid
            + ",uid=" + uid + ",memorySize=" + memorySize / 1024 + "MB");

        // log(JSON.stringify(memoryInfo))
    })

}

myFunc.findOne = function (str, time, fuzz) {
    if (fuzz != undefined) {
        return textContains(str).findOne(time);
    }
    if (time == undefined) {
        time = 3000
    }
    return text(str).findOne(time);
}

myFunc.findOnce = function (str, fuzz) {
    if (fuzz != undefined) {
        return textContains(str).findOnce();
    }
    return text(str).findOnce();
}

myFunc.find = function (str, fuzz) {
    if (fuzz != undefined) {
        return textContains(str).find();
    }
    return text(str).find();
}

myFunc.findTextDesc = function (str, fuzz) {
    var list = [];
    if (fuzz == false) {
        list = list.concat(text(str).find());
        list = list.concat(desc(str).find());
    } else {
        list = list.concat(textContains(str).find());
        list = list.concat(descContains(str).find());
    }
    return list;
}

myFunc.findOnceTextDesc = function (str, fuzz) {
    var ret = null
    if(fuzz) {
        ret = textContains(str).findOnce();
        if(ret == null) {
            ret = descContains(str).findOnce();
        }
    } else {
        ret = text(str).findOnce();
        if(ret == null) {
            ret = desc(str).findOnce();
        }
    }
    return ret;
}

myFunc.findByUpDownText = function(comBox, str2, up, down, fuzzy) {
    if (!comBox) {
        return null;
    }
    var b = null;
    if (fuzzy) {
        b = textContains(str2).boundsInside(0, comBox.bounds().top + up, device.width, comBox.bounds().bottom + down).findOnce();
    } else {
        b = text(str2).boundsInside(0, comBox.bounds().top + up, device.width, comBox.bounds().bottom + down).findOnce();
    }
    return b;
}

myFunc.findByUpDownButton = function(comBox, str2, up, down, fuzzy) {
    if (!comBox) {
        return null;
    }
    var b = null;
    if (fuzzy) {
        b = textContains(str2).boundsInside(0, comBox.bounds().top + up, device.width, comBox.bounds().bottom + down).findOnce();
    } else {
        b = text(str2).boundsInside(0, comBox.bounds().top + up, device.width, comBox.bounds().bottom + down).findOnce();
    }
    return b;
}

myFunc.findByUpDownClassName = function(comBox, str2, up, down) {
    if (!comBox) {
        return null;
    }
    var b = className(str2).boundsInside(0, comBox.bounds().top + up, device.width, comBox.bounds().bottom + down).findOnce();
    return b;
}

myFunc.showUI = function(b) {
    var q = floaty.rawWindow(
        //<vertical bg="#ff0000">
            <button id="but"  alpha="0.7" text="*" bg="#888888" />
        //</vertical >
    );
    q.setPosition(b.bounds().left, b.bounds().top-118);
    q.setTouchable(true);
    q.exitOnClose();
    q.setSize(b.bounds().right-b.bounds().left, b.bounds().bottom-b.bounds().top);

    q.but.click(function () {
        q.close();
    });
    setTimeout(()=>{
        q.close();
    }, 5000);
}


myFunc.waitToNotFind = function (str, fuzz) {
    while (myFunc.findOnce(str, fuzz) != null) {
        sleep(1000);
    }
}

myFunc.setMusicVolume = function (volumeLevel) {
    importClass(android.media.AudioManager)
    importClass(android.content.Context)
    var type = AudioManager.STREAM_MUSIC
    var audioManager = context.getSystemService(Context.AUDIO_SERVICE);
    audioManager.requestAudioFocus(null, type, AudioManager.AUDIOFOCUS_GAIN);
    var lvNow = device.getMusicVolume()
    var maxVolume = audioManager.getStreamMaxVolume(type)
    var shang = volumeLevel / 100 * maxVolume
    var action = AudioManager.ADJUST_RAISE
    var num = shang - lvNow;
    if (lvNow > shang) {
        //调小
        action = AudioManager.ADJUST_LOWER
        num = lvNow - shang;
    }
    for (let index = 0; index < num; index++) {
        audioManager.adjustStreamVolume(type, action, AudioManager.FLAG_SHOW_UI);
        sleep(100)
    }

}
/**
 * 真人模拟滑动函数
 * 
 * 传入值：起点终点坐标
 * 效果：模拟真人滑动
 */
myFunc.randomSwipe = function(sx,sy,ex,ey) {
    //设置随机滑动时长范围
    var timeMin=500
    var timeMax=1500
    //设置控制点极限距离
    var leaveHeightLength=500
    
    //根据偏差距离，应用不同的随机方式
    if(Math.abs(ex-sx)>Math.abs(ey-sy)){
        var my=(sy+ey)/2
        var y2=my+random(0,leaveHeightLength)
        var y3=my-random(0,leaveHeightLength)
    
        var lx=(sx-ex)/3
        if(lx<0){lx=-lx}
        var x2=sx+lx/2+random(0,lx)
        var x3=sx+lx+lx/2+random(0,lx)
    }else{
        var mx=(sx+ex)/2
        var y2=mx+random(0,leaveHeightLength)
        var y3=mx-random(0,leaveHeightLength)

        var ly=(sy-ey)/3
        if(ly<0){ly=-ly}
        var y2=sy+ly/2+random(0,ly)
        var y3=sy+ly+ly/2+random(0,ly)
    }

    //获取运行轨迹，及参数
    var time=[0,random(timeMin,timeMax)]
    var track= bezierCreate(sx,sy,x2,y2,x3,y3,ex,ey)
    
    log("随机控制点A坐标："+x2+","+y2)
    log("随机控制点B坐标："+x3+","+y3)
    log("随机滑动时长："+time[1])
    
    //滑动
    gestures(time.concat(track))
}


function bezierCreate(x1,y1,x2,y2,x3,y3,x4,y4){
    //构建参数
    var h=100;
    var cp=[{x:x1,y:y1+h},{x:x2,y:y2+h},{x:x3,y:y3+h},{x:x4,y:y4+h}];
    var numberOfPoints = 100;
    var curve = [];
    var dt = 1.0 / (numberOfPoints - 1);
    
    //计算轨迹
    for (var i = 0; i < numberOfPoints; i++){
        var ax, bx, cx;
        var ay, by, cy;
        var tSquared, tCubed;
        var result_x, result_y;
    
        cx = 3.0 * (cp[1].x - cp[0].x);
        bx = 3.0 * (cp[2].x - cp[1].x) - cx;
        ax = cp[3].x - cp[0].x - cx - bx;
        cy = 3.0 * (cp[1].y - cp[0].y);
        by = 3.0 * (cp[2].y - cp[1].y) - cy;
        ay = cp[3].y - cp[0].y - cy - by;
    
        var t=dt*i
        tSquared = t * t;
        tCubed = tSquared * t;
        result_x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
        result_y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
        curve[i] = {
            x: result_x,
            y: result_y
        };
    }

    //轨迹转路数组
    var array=[];
    for (var i = 0;i<curve.length; i++) {
        try {
            var j = (i < 100) ? i : (199 - i);
            xx = parseInt(curve[j].x)
            yy = parseInt(Math.abs(100 - curve[j].y))
        } catch (e) {
            break
        }
        array.push([xx, yy])
    }
    
    return array
}

myFunc.jiamikey = storages.create('jiamikey');
myFunc.key = new $crypto.Key(myFunc.jiamikey.get('16位数据加密keyqq+从9倒数'));
myFunc.jiami = function (key, str) {
    let aes = $crypto.encrypt(str, myFunc.key, "AES/ECB/PKCS5padding");
    myFunc.jiamikey.put(key, aes)
}
myFunc.jiemi = function (saveKey) {
    let aes = myFunc.jiamikey.get(saveKey)
    return $crypto.decrypt(aes, myFunc.key, "AES/ECB/PKCS5padding", {output: 'string'});
}

myFunc.initDateStorage = function(){
    let now = new Date();
    let hour = now.getHours();
    let date = now.getDate();
    let month = now.getMonth();
    let year = now.getFullYear();
    let storage = storages.create(year + '-' + month + '-' + date);
    if(hour == 15){
        let yesterdayStorage = storages.create(year + '-' + month + '-' + (date - 1));
        yesterdayStorage.clear()
    }
    return storage;
}

module.exports = myFunc;
